package myTest;

import java.sql.Connection;
import java.util.Date;
import java.util.Map;

import com.opensymphony.oscache.base.NeedsRefreshException;
import com.opensymphony.oscache.general.GeneralCacheAdministrator;

public class OscacheTest{
	public static void main(String[] args) {
		OscacheTest2 oscacheTest = new OscacheTest2();
		oscacheTest.put("12", "18");
		System.out.println(oscacheTest.get("12"));
		try {
			Thread.sleep(5000);
			System.out.println(oscacheTest.get("12"));
			Thread.sleep(5000);
			System.out.println(oscacheTest.get("12"));
			Thread.sleep(5000);
			System.out.println(oscacheTest.get("12"));
			Thread.sleep(5000);
			System.out.println(oscacheTest.get("12"));
			Thread.sleep(5000);
			System.out.println(oscacheTest.get("12"));
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}



class OscacheTest2 extends GeneralCacheAdministrator{
		/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
		private final static int refreshPeriod = 1; //过期时间  单位为妙
	// 添加被缓存的对象;
		public void put(String key, Object value) {
			this.putInCache(key, value);
		}

		// 删除被缓存的对象;
		public void remove(String key) {
			this.flushEntry(key);
		}

		// 删除所有被缓存的对象;
		public void removeAll(Date date) {
			this.flushAll(date);
		}

		public void removeAll() {
			this.flushAll();
		}

		// 获取被缓存的对象; key 为标号   
		public String get(String key) {
			try {
				return (String) this.getFromCache(key,refreshPeriod);
			} catch (NeedsRefreshException e) {
				System.out.println("获取"+key+"失败");
				//this.put("12", "19");
				//this.cancelUpdate(key);
			}
					
			return null;
		}
}
